package genetic.q2;


import genetic.Genotype;

import java.util.Random;
import java.util.Vector;

public class PrisonerGenotype implements Genotype{

	public Vector<boolean[]> instance;
	public int _size;
	public int total;
	public int place;
	public int score;
	public int[] memory;
	public PrisonerGenotype(int size){
		_size = size;
		place=0;
		memory = new int[_size*2];
		instance = new Vector<boolean[]>();
		for(int i=0; i<=size;i++){
			int mat =(int) Math.pow(2, i*2);
			boolean[] tmp = new boolean[mat];
			for(int j=0; j<mat;j++){
				Random r = new Random();
				tmp[j] = r.nextBoolean();
			}
			instance.add(tmp);
		}
	}

	public PrisonerGenotype(){

	}

	public PrisonerGenotype clone(){
		PrisonerGenotype g = new PrisonerGenotype(_size);
		g.instance=instance;
		return g;
	}

	@Override
	public String toString() {
		String ans = "";
		for (int i=0; i<instance.size(); i++){
			for (int j=0; j<instance.elementAt(i).length; j++){
			if(instance.elementAt(i)[j])
				ans = ans+"1";
				else ans = ans+"0";
			}
			ans= ans+" | ";
		}
		return ans;
	}

	@Override
	public int[] getIntString() {
		// TODO Auto-generated method stub
		return null;
	}

	public void update(int turn, boolean f, boolean s) {
		int first=0, second=0;
		total++;
		if(f) first=1;
		if(s) second=1;
		if(f){
			if(s)
				score = score+3;
		}else{
			if(s)
				score = score+5;
			else score++;
		}
		if(_size==1){
				memory[0]=first;
				memory[1]=second;
		}else if(_size==2){
			if(turn==0){
				memory[place]=first;
				memory[(place+1)%4]=second;
			}
			else if(turn==1){
				memory[(place+2)%4]=first;
				memory[(place+3)%4]=second;
			}
			else{
				memory[place]=first;
				memory[(place+1)%4]=second;
				place = (place+2)%4;
			}
		}else{
			if(turn==0){
				memory[0]=first;
				memory[1]=second;
			}
			else if(turn==1){
				memory[(place+2)%6]=first;
				memory[(place+3)%6]=second;
			}
			else if(turn==2){
				memory[(place+4)%6]=first;
				memory[(place+5)%6]=second;
			}
			else{
				memory[place]=first;
				memory[(place+1)%6]=second;
				place = (place+2)%6;
			}
		}
	}

	public boolean returnChoise(int turn) {
		if(_size==1){
			if(turn==0){
				return instance.elementAt(0)[0];
			}
			else{
				int choice = memory[0]+ (2*memory[1]);
				return instance.elementAt(1)[choice];
			}
		}else if(_size==2){
			if(turn==0){
				return instance.elementAt(0)[0];
			}
			else if(turn==1){
				int choice = memory[0]+ (2*memory[1]);
				return instance.elementAt(1)[choice];
			}
			else{
				int choice = memory[place]+ (2*memory[(place+1)%4]) + (4*memory[(place+2)%4]) + (8*memory[(place+3)%4]);
				return instance.elementAt(2)[choice];
			}
		}else{
			if(turn==0){
				return instance.elementAt(0)[0];
			}
			else if(turn==1){
				int choice = memory[0]+ (2*memory[1]);
				return instance.elementAt(1)[choice];
			}
			else if(turn==2){
				int choice = memory[place]+ (2*memory[(place+1)%4]) + (4*memory[(place+2)%4]) + (8*memory[(place+3)%4]);
				return instance.elementAt(2)[choice];
			}
			else{
				int choice = memory[place]+ (2*memory[(place+1)%6]) + (4*memory[(place+2)%6]) + (8*memory[(place+3)%6]) + (16*memory[(place+4)%6]) + (32*memory[(place+5)%6]);
				return instance.elementAt(3)[choice];
			}
		}
	}

}
